#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class SkillProvider : CY.GFive.Core.Providers.DALProvider.ISkillProvider
	{
		#region Sql
		private static readonly string SqlSelectSkill = "SELECT [Id], [SkillLvl],[StaffCode],[AddDate],[Code] FROM [Skill] WHERE  [Id]=@Id";

		private static readonly string SqlInsertSkill = "INSERT INTO [Skill]([SkillLvl],[StaffCode],[AddDate],[Code]) values(@SkillLvl,@StaffCode,@AddDate,@Code);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateSkill = "UPDATE [Skill] SET [SkillLvl] = @SkillLvl,[StaffCode] = @StaffCode,[AddDate] = @AddDate,[Code] = @Code WHERE  [Id]=@Id";

		private static readonly string SqlDeleteSkill = "DELETE [Skill] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllSkill = "SELECT [Id], [SkillLvl],[StaffCode],[AddDate],[Code] FROM [Skill]";
		#endregion

		#region ISkillProvider Members
		public Core.Business.Skill Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectSkill);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Skill skill = new Core.Business.Skill();

				if (!reader.IsDBNull(0)) skill.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) skill.SkillLvl = reader.GetString(1);
				if (!reader.IsDBNull(2)) skill.StaffCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) skill.AddDate = reader.GetDateTime(3);
				if (!reader.IsDBNull(4)) skill.Code = reader.GetString(4);

				return skill;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Skill skill)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@SkillLvl", SqlDbType.NVarChar, skill.SkillLvl);
			sql.AddParameter("@StaffCode", SqlDbType.NVarChar, skill.StaffCode);
			sql.AddParameter("@AddDate", SqlDbType.DateTime, skill.AddDate);
			sql.AddParameter("@Code", SqlDbType.NVarChar, skill.Code);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertSkill);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) skill.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Skill skill)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@SkillLvl", SqlDbType.NVarChar, skill.SkillLvl);
			sql.AddParameter("@StaffCode", SqlDbType.NVarChar, skill.StaffCode);
			sql.AddParameter("@AddDate", SqlDbType.DateTime, skill.AddDate);
			sql.AddParameter("@Code", SqlDbType.NVarChar, skill.Code);
			sql.AddParameter("@Id", SqlDbType.Int, skill.Id);
			sql.ExecuteSql(SqlUpdateSkill);
		}

		public void Delete(Core.Business.Skill skill)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, skill.Id);
			sql.ExecuteSql(SqlDeleteSkill);
		}

		public IList<Core.Business.Skill> GetAllSkill()
		{
			IList<Core.Business.Skill> skilllist = new List<Core.Business.Skill>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllSkill);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Skill skill = new Core.Business.Skill();

					if (!reader.IsDBNull(0)) skill.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) skill.SkillLvl = reader.GetString(1);
					if (!reader.IsDBNull(2)) skill.StaffCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) skill.AddDate = reader.GetDateTime(3);
					if (!reader.IsDBNull(4)) skill.Code = reader.GetString(4);

					skill.MarkOld();
					skilllist.Add(skill);
				}
				reader.Close();
			}
			return skilllist;
		}
		#endregion
	}
}
